This is a fork of the official json-typedef-js that provides improved ESM support while (hopefully) maintaining compatibility with legacy CommonJS stuff.
jtd: JSON Validation for JavaScript
![npm](https://img.shields.io/npm/v/jtd)
JSON Type Definition, aka RFC
8927, is an easy-to-learn, standardized
way to define a schema for JSON data. You can use JSON Typedef to portably
validate data across programming languages, create dummy data, generate code,
and more.
This jtd
package is a JavaScript / TypeScript implementation of JSON Type
Definition. It lets you validate input data against JSON Type Definition
schemas. jtd
works in Node.js and web browsers.
If you're looking to generate code from schemas, check out "Generating
TypeScript from JSON Typedef schemas" in the JSON Typedef docs.
Installation
You can install this package with npm
:
npm install jtd
Or with yarn
:
yarn add jtd
Documentation
Detailed API documentation is available online at:
https://jsontypedef.github.io/json-typedef-js/index.html
For more high-level documentation about JSON Typedef in general, or JSON Typedef
in combination with JavaScript in particular, see:
Basic Usage
Here's an example of how you can use this package to validate JSON data against
a JSON Typedef schema:
import { Schema, validate } from "jtd";
const schema = {
properties: {
name: { type: "string" },
age: { type: "uint32" },
phones: {
elements: { type: "string" },
},
},
} as Schema;
console.log(
validate(schema, {
name: "John Doe",
age: 43,
phones: ["+44 1234567", "+44 2345678"],
})
);
console.log(
validate(schema, {
age: "43",
phones: ["+44 1234567", 442345678],
})
);
Advanced Usage: Limiting Errors Returned
By default, jtd.validate
returns every error it finds. If you just care about
whether there are any errors at all, or if you can't show more than some number
of errors, then you can get better performance out of jtd.validate
using the
maxErrors
option.
For example, taking the same example from before, but limiting it to 1 error, we
get:
console.log(
validate(
schema,
{
age: "43",
phones: ["+44 1234567", 442345678],
},
{ maxErrors: 1 }
)
);
Advanced Usage: Handling Untrusted Schemas
If you want to run jtd
against a schema that you don't trust, then you should:
-
Ensure the schema is well-formed, using jtd.isSchema
and
jtd.isValidSchema
. isSchema
does basic "type" checking (and in
TypeScript, it acts as a type guard for the Schema
type), while
isValidSchema
validates things like making sure all ref
s have
corresponding definitions.
-
Call jtd.validate
with the maxDepth
option. JSON Typedef lets you write
recursive schemas -- if you're evaluating against untrusted schemas, you
might go into an infinite loop when evaluating against a malicious input,
such as this one:
{
"ref": "loop",
"definitions": {
"loop": {
"ref": "loop"
}
}
}
The maxDepth
option tells jtd.validate
how many ref
s to follow
recursively before giving up and throwing jtd.MaxDepthExceededError
.
Here's an example of how you can use jtd
to evaluate data against an untrusted
schema:
import { isSchema, isValidSchema, Schema, validate } from "jtd";
function validateUntrusted(schema: unknown, data: unknown): boolean {
if (!isSchema(schema) || !isValidSchema(schema)) {
throw new Error("invalid schema");
}
return validate(schema, data, { maxDepth: 32 }).length === 0;
}
validateUntrusted({ type: "string" }, "foo");
validateUntrusted({ type: "string" }, null);
validateUntrusted({ type: "nonsense" }, null);
validateUntrusted(
{
ref: "loop",
definitions: {
loop: {
ref: "loop",
},
},
},
null
);